package main

import (
	"fmt"
	"server/utils"
	"time"

	"github.com/gofrs/uuid"
	"github.com/golang-jwt/jwt/v5"
)

var jwtSecret = []byte("leorunloop")

type MyClaims struct {
	Uid string `json:"uid"`
	jwt.RegisteredClaims
}

func GenerateToken() string {
	claims := MyClaims{
		"1",
		jwt.RegisteredClaims{
			Issuer:    "leorunloop",
			Subject:   "user_auth",
			Audience:  jwt.ClaimStrings{"server.com"},
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(7 * 24 * time.Hour)),
			NotBefore: jwt.NewNumericDate(time.Now()),
			IssuedAt:  jwt.NewNumericDate(time.Now()),
			ID:        uuid.NamespaceDNS.String(),
		},
	}
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	tokenString, err := token.SignedString(jwtSecret)
	if err != nil {
		panic(err)
	}
	return tokenString
}

func ParseToken(tokenString string) bool {
	token, err := jwt.ParseWithClaims(tokenString, &MyClaims{}, func(t *jwt.Token) (interface{}, error) {
		return []byte(jwtSecret), nil
	})
	if err != nil {
		return false
	} else if claims, ok := token.Claims.(*MyClaims); ok {
		fmt.Println(claims.Uid, claims.RegisteredClaims.Issuer)
	} else {
		fmt.Println(2222)
	}
	return token.Valid
}
func main() {
	_, token, _ := utils.GenerateToken("uid00001", true)
	time.Sleep(1 * time.Second)
	fmt.Println(token)
	// result, _ := utils.ParseToken(token)
	// fmt.Println(result.Uid, result.RegisteredClaims.Issuer)
	utils.RefreshToken(token)
}
